import requests
from lxml import etree
import os

os.mkdir('./素材')
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
# for i in range(1,3):   # 共有几页自己改改就行了~
#     if i == 1:
#         url = 'http://sc.chinaz.com/jianli/free.html'
#         continue
#     else:
#         url = f'http://sc.chinaz.com/jianli/free_%d.html' % i
        #获取页面数据并设置编码
# base_url = 'http://sc.chinaz.com/jianli/free'
for i in range(1,5):
        if i == 1:
            url = base_url + '.html'
        else:
            url = base_url + '_' + str(i) + '.html'
        all_moban_text = requests.get(url,headers)
        all_moban_text.encoding = 'utf-8'
        all_moban_text = all_moban_text.text

        tree = etree.HTML(all_moban_text)
        div_list = tree.xpath('//*[@id="container"]/div')
        for each_url in div_list:
            file_name = each_url.xpath('./p/a/text()')[0] + ".rar"
            each_url = each_url.xpath('./a/@href')[0]
            detail_text = requests.get(url=each_url,headers=headers).text
            tree_2 = etree.HTML(detail_text)
            download_url = tree_2.xpath('//*[@id="down"]/div[2]/ul/li[1]/a/@href')[0]
            download = requests.get(url=download_url,headers=headers).content
            file_path = './素材/' + file_name
            with open(file_path,'wb') as f:
                f.write(download)
                print(file_name,"下载成功！")



